package com.oi.test01.hw2;

import com.oi.test01.entity.Student;

import java.beans.Introspector;
import java.beans.PropertyDescriptor;

/**
 * @author bin
 * @date 2025 - 03 -25
 **/
public class MainTest03 {
    public static void main(String[] args) throws Exception {
        Student stu = new Student("徐文斌", 18, 70);
        Student stu2 = new Student();
        copyPropreties(stu, stu2);
        System.out.println(stu2.toString());
    }
    public static void copyPropreties(Object source,Object target) throws Exception{
        PropertyDescriptor[] sourcePD = Introspector.getBeanInfo(source.getClass(),Object.class).getPropertyDescriptors();
        PropertyDescriptor[] targetPD = Introspector.getBeanInfo(target.getClass(),Object.class).getPropertyDescriptors();
        for (PropertyDescriptor sourcePd : sourcePD) {
            for (PropertyDescriptor targetPd : targetPD) {
                if (sourcePd.getName().equals(targetPd.getName())) {
                    Object value = sourcePd.getReadMethod().invoke(source);
                    if (value != null) {
                        targetPd.getWriteMethod().invoke(target, value);
                    }
                }
            }
        }
    }
}
